import axios from 'axios'
// import store from '@store/'
import storage from 'store'
import notification from 'ant-design-vue/es/notification'
import {
  VueAxios
} from './axios'
import {
  ACCESS_TOKEN
} from '@/store/mutation-types'

import {
  encrypt
} from './util' //Rsa加密

// 创建 axios 实例
const request = axios.create({
  // API 请求的默认前缀
  baseURL: process.env.VUE_APP_API_BASE_URL, timeout: 1000 * 60 * 5 // 请求超时时间
})

// 异常拦截处理器
const errorHandler = (error) => {
  if (error.response) {
    const data = error.response.data
    if (error.response.status === 506) {
      notification.error({
        message: '文件未找到'
      })
    }
  }
  return Promise.reject(error)
}

// request interceptor
request.interceptors.request.use(config => {
  const token = storage.get(ACCESS_TOKEN)
  // 如果 token 存在
  // 让每个请求携带自定义 token 请根据实际情况自行修改
  if (token) {
    config.headers['token'] = token
  }
  if (config.url.includes('file/upload')) {
    config.headers['Content-Type'] = 'multipart/form-data'
  } else if (config.url.includes('file/get')) {
    config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
  } else {
    config.headers['Content-Type'] = 'application/json;charset=utf-8'
  }
  if (config.url.includes('file/get') || config.url.includes('docData/download') || config.url.includes('docData/downloadAll')) {
    config.responseType = 'blob'
  }
  if ((config.method == 'post' || config.method == 'put') && (config.url == '/sys-center/api/login' || config.url == '/sys-center/api/system/updatePassword')) {
    config.data = encrypt(JSON.stringify(config.data))
  }
  return config
}, errorHandler)

// response interceptor
request.interceptors.response.use((response) => {
  console.log('请求返回', response.data)
  const data = response.data
  if (data.code == 210) {
    notification.error({
      message: '用户名密码错误', description: data.msg
    })
    return Promise.reject(data.msg)
  } else if (data.code == 220) {
    notification.error({
      message: '请求错误', description: data.msg
    })
    return Promise.reject(data.msg)
  } else if (data.code == 400) {
    notification.error({
      message: '参数错误', description: data.msg
    })
    return Promise.reject(data.msg)
  } else if (data.code == 500) {
    notification.error({
      message: '服务器异常', description: data.msg
    })
    return Promise.reject(data.msg)
  } else if (data.code == 505) {
    notification.error({
      message: '错误', description: data.msg, style: {
        wordWrap: 'break-word'
      }
    })
    return Promise.reject(data.msg)
  } else if (data.code == 401) {
    notification.error({
      message: '暂无权限访问', description: data.msg
    })
    return Promise.reject(data.msg)
  } else if (data.code == 405) {
    storage.remove(ACCESS_TOKEN)
    store.dispatch('Logout').then(() => {
      next({
        path: '/user/login'
      })
    })
  } else if (data.code == 406) {
    storage.remove(ACCESS_TOKEN)

    store.dispatch('Logout').then(() => {
      next({
        path: '/user/login'
      })
    })
  } else if (data.code == 200) {
    if (typeof data.data === 'object') {
      for (const key in data.data) {
        if (data.data.hasOwnProperty(key) && data.data[key] === null) {
          data.data[key] = undefined
        }
      }
    }
    console.log('请求最终返回', data)
    return data
  } else if (!data.code) {
    console.log('请求最终返回', data)
    return data
  }
}, errorHandler)

const installer = {
  vm: {}, install(Vue) {
    Vue.use(VueAxios, request)
  }
}

export default request

export {
  installer as VueAxios, request as axios
}